package com.whmall.service.crm.module.companycertificatechemical;

import com.whmall.component.entity.dto.PageReqDTO;
import com.whmall.component.entity.dto.PageRspDTO;
import com.whmall.component.enumeration.OperatorTypeEnum;
import com.whmall.component.entity.dto.CompanyKeyFieldDTO;
import com.whmall.service.crm.entity.dto.companycertificatechemical.CompanyCertificateChemicalApprovePageItemRspDTO;
import com.whmall.service.crm.entity.dto.companycertificatechemical.CompanyCertificateChemicalPageItemRspDTO;
import com.whmall.service.crm.module.company.service.CompanyService;
import com.whmall.service.crm.module.companybusinesscertificate.dao.CompanyBusinessCertificateMapper;
import com.whmall.service.crm.module.companybusinesscertificate.entity.CompanyBusinessCertificateDO;
import com.whmall.service.crm.module.companybusinesscertificate.entity.qo.CompanyBusinessCertificateQO;
import com.whmall.service.crm.module.companybusinesscertificate.enumeration.BusinessCertificateTypeEnum;
import com.whmall.service.crm.module.companycertificatechemical.entity.CompanyCertificateChemicalApproveDO;
import com.whmall.service.crm.module.companycertificatechemical.entity.qo.CompanyCertificateChemicalQO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.stream.Collectors;

/**
 * 公司证照化合物视图构建器
 *
 * <p>
 * 此组件用于组合返回数据所需要的视图模型
 * </p>
 *
 * @author alex
 * @since 2020/12/19
 */
@Component
@RequiredArgsConstructor
public class CompanyCertificateChemicalViewBuilder {
    private final CompanyCertificateChemicalRepository companyCertificateChemicalRepository;
    private final CompanyBusinessCertificateMapper certificateMapper;
    private final CompanyService companyService;

    /**
     * 分页获取公司证照化合物列表
     * @param pageRequest
     * @param qo
     * @return
     */
    public PageRspDTO<CompanyCertificateChemicalPageItemRspDTO> buildAssociatePage(PageReqDTO pageRequest,
             CompanyCertificateChemicalQO qo) {
        return companyCertificateChemicalRepository.findAssociatePage(pageRequest, qo)
                .map(e -> {
                    CompanyCertificateChemicalPageItemRspDTO dto = new CompanyCertificateChemicalPageItemRspDTO();
                    dto.setId(e.getId());
                    dto.setChemicalId(e.getChemicalId());
                    dto.setUserId(e.getUserId());
                    dto.setOperatorType(OperatorTypeEnum.fromValue(e.getCreateType()));
                    dto.setCreateId(e.getCreateId());
                    dto.setCreateTime(e.getCreateTime());
                    dto.setVerified(e.getVerified());
                    dto.setVerifyId(e.getVerifyId());
                    dto.setVerifyTime(e.getVerifyTime());
                    dto.setNotPassCause(e.getNotPassCause());
                    dto.setCertificateId(e.getCertificateId());
                    dto.setCompanyId(e.getCompanyId());
                    return dto;
                });
    }

    /**
     * 根据条件分页获取公司证照化合物审批列表
     *
     * @param pageRequest 分页请求
     * @param criteria    查询条件
     * @return 分页数据
     */
    public PageRspDTO<CompanyCertificateChemicalApprovePageItemRspDTO> buildApprovePage(PageReqDTO pageRequest,
                                                                                        CompanyCertificateChemicalApproveQueryCriteria criteria) {
        PageRspDTO<CompanyCertificateChemicalApproveDO> approvePage = companyCertificateChemicalRepository.findApprovePage(pageRequest, criteria);
        if (approvePage.getRecords().isEmpty()) {
            return approvePage.map(e -> null);
        }


        Map<Long, CompanyBusinessCertificateDO> certificateMap =
                certificateMapper.listBy(CompanyBusinessCertificateQO.builder()
                        .idIn(approvePage.getRecords().stream()
                                .map(CompanyCertificateChemicalApproveDO::getId)
                                .collect(Collectors.toList()))
                        .build())
                        .stream()
                        .collect(Collectors.toMap(CompanyBusinessCertificateDO::getId, e -> e));

        Map<Long, String> companyNameMap = companyService.listByIds(approvePage.getRecords().stream()
                .map(CompanyCertificateChemicalApproveDO::getCompanyId)
                .distinct()
                .collect(Collectors.toList()))
                .values()
                .stream()
                .collect(Collectors.toMap(CompanyKeyFieldDTO::getId,
                        CompanyKeyFieldDTO::getName));


        return approvePage.map(e -> {
            CompanyCertificateChemicalApprovePageItemRspDTO dto = new CompanyCertificateChemicalApprovePageItemRspDTO();

            dto.setCompanyId(e.getCompanyId());
            dto.setCompanyName(companyNameMap.get(e.getCompanyId()));
            dto.setCertificateId(e.getId());
            dto.setCertificateType(BusinessCertificateTypeEnum.fromNullableValue(e.getCertificateType()));

            CompanyBusinessCertificateDO certificate = certificateMap.get(e.getId());
            if (null != certificate) {
                dto.setCertificateNumber(certificate.getCertificateNumber());
                dto.setEffectiveStartDate(certificate.getEffectiveStartDate());
                dto.setEffectiveEndDate(certificate.getEffectiveEndDate());
                dto.setCreateType(OperatorTypeEnum.fromValue(certificate.getCreateType()));
                dto.setCreateId(certificate.getCreateId());
                dto.setCreateTime(certificate.getCreateTime());
                dto.setIsEffective(certificate.getEffective().equals(1));
            }
            return dto;
        });
    }
}
